import error
from concurrent.futures import ThreadPoolExecutor

from dependencies.myMySQL.mysqDB import MySQLOrm
from dependencies.myMongo.monogUtil import MongoUtil
from utils import *
from sql.selfInlay import *

mysql = MySQLOrm()
execute = ThreadPoolExecutor()


def query(params):
    """
        查询mongo数据
    """

    mongo = MongoUtil._get_connection('selfInlay', '19')
    return mongo.find(params)


def execute_insert_self_inlay():
    """
        处理商品出库自留镶嵌实现方法
        第一步  MySQL查询所有还有没有回库的自留镶嵌订单
        第二步  Mongodb保存
    """

    result, count = mysql.get_all(QUERY_ALL)
    if count > 0:
        # 线程池 异步 去处理数据写入
        # execute.submit(result_insert_to_mongo, result)
        result_insert_to_mongo(result)
    return count


def result_insert_to_mongo(result_data):
    """
        处理客订维修和镶嵌保存到mongodb的方法
    """
    mongo =  MongoUtil._get_connection('selfInlay', get_consumer_now__format('%Y%m%d'))
    if mongo.insert_many(convert_decimal_to_float(result_data)).acknowledged:
        print("======== mongodb-orderInlay-self-inlay 自留镶嵌插入成功======== ")
